import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;

public class SimpleSolution {

	Scanner in;
	PrintWriter out;

	class Number implements Comparable<Number> {

		int value;
		int digitSum;

		public Number(int value) {
			this.value = value;
			this.digitSum = getDigitSum();
		}

		private int getDigitSum() {
			int curValue = value;
			int sum = 0;
			while (curValue > 0) {
				sum += curValue % 10;
				curValue /= 10;
			}
			return sum;
		}

		@Override
		public int compareTo(Number second) {
			if (this.digitSum != second.digitSum) {
				return this.digitSum - second.digitSum;
			} else {
				return this.value - second.value;
			}
		}
	}

	private int getDigitLen(int num) {
		return Integer.toString(num).length();
	}

	void solve() {
		int n = in.nextInt();
		int m = in.nextInt();
		int maxNum = (int) Math.pow(10, n);
		Number a[] = new Number[maxNum];
		for (int i = 1; i <= maxNum; i++) {
			a[i - 1] = new Number(i);
		}
		Arrays.sort(a);
		for (int i = 0; i < m; i++) {
			int k = in.nextInt();
			int kLen = getDigitLen(k);
			int ans = 0;
			int curPos = 0;
			while (true) {
				if (a[curPos].value == k) {
					out.println(ans);
					break;
				}
				if (getDigitLen(a[curPos].value) > kLen) {
					ans++;
				}
				curPos++;
			}
		}
	}

	void run() {
		in = new Scanner(System.in);
		out = new PrintWriter(System.out);
		try {
			solve();
		} finally {
			out.close();
		}
	}

	public static void main(String[] args) {
		new SimpleSolution().run();
	}

}
